<?php

class BaseController extends Controller {

    protected $pageSize = 10; 
    protected function __construct() {

    }
    
    /**
     * Setup the layout used by the controller.
     * @return void
     */
    protected function setupLayout() {
        if ( ! is_null($this->layout)) {
            $this->layout = View::make($this->layout);
        }
    }
    

    /**
     * Setup layout for ajax html response
     * @author Thuan Truong
     * @param View $view
     * @param string $title
     * @return Response
     */
    protected function ajaxResponse($view, $title = null) {
        $layout = View::make('layouts.ajax');
        $layout->content = $view;

        $response = Response::make($layout);
        $response->header('Cache-Control', 'nocache, no-store, max-age=0, must-revalidate');
        $response->header('Pragma', 'no-cache');
        $response->header('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT');
        if (!empty($title)) {
            $response->header('X-Page-Title', "Mulberry Lane | $title");
        }

        return $response;
    }
    
    /**
     * check user is login
     * @author Thuan Truong
     */
    public static function isLogin() {
        $user = Sentry::getUser();
        if (empty($user))
            return false;
        return true;
    }
    
    /**
     * check active user
     * @author Thuan Truong
     */
    public static function isActive() {
        $user = Sentry::getUser();
        if (!empty($user)) {
            if ($user->activated == 1)
                return true;
            return false;
        }
        return false;
    }
    
    /**
     * get role for user
     * @author Thuan Truong
     */
    public static function getRole() {
        $user = Sentry::getUser();
        if (empty($user)) {
            return View::make('home');
        }
        $groups = $user->getGroups();
        return $groups[0]->id;
    }

	/**
	 * get all provinces in database
	 * @author Thuan Truong
	 */
	public static function getAllProvinces () {
		$provinces = Province::whereRaw('status = ? and is_deleted = ?', array(1,0))->get();
		return $provinces;
	}
	
	
	/**
	 * get all catogories in database
	 * @author Thuan Truong
	 */
	public static function getAllCategories () {
		$categories = Category::whereRaw('status = ? and is_deleted = ?', array(1,0))->get();
		return $categories;
	}
	
	/**
	 * create all prices
	 * @author Thuan Truong
	 */
	public static function getAllPrices () {
		$prices = array(
			'1 - 2 triệu',
			'2 - 3 triệu',
			'3 - 5 triệu',
			'5 - 7 triệu',
			'7 - 10 triệu',
			'10 - 15 triệu',
			'Trên 15 triệu',
			'Thỏa thuận',
		);
		return $prices;
	}
	
	/**
	 * get term website
	 * @author Thuan Truong
	 */
	public static function getTermWebsite () {
		return $term = Term::first();
	}
}
